﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
/// <summary>
/// Summary description for clsBaiThi
/// </summary>
public class clsBaiThi
{
    DataClassesDataContext db = new DataClassesDataContext();
    public clsBaiThi()
    {

    }
    private string STT;

    public string STT1
    {
        get { return STT; }
        set { STT = value; }
    }
    private string MaCH;

    public string MaCH1
    {
        get { return MaCH; }
        set { MaCH = value; }
    }
    private string Noidung;

    public string Noidung1
    {
        get { return Noidung; }
        set { Noidung = value; }
    }
    private string Pa1;

    public string Pa11
    {
        get { return Pa1; }
        set { Pa1 = value; }
    }
    private string Pa2;

    public string Pa21
    {
        get { return Pa2; }
        set { Pa2 = value; }
    }
    private string Pa3;

    public string Pa31
    {
        get { return Pa3; }
        set { Pa3 = value; }
    }
    private string Pa4;

    public string Pa41
    {
        get { return Pa4; }
        set { Pa4 = value; }
    }
    private string Traloi;

    public string Traloi1
    {
        get { return Traloi; }
        set { Traloi = value; }
    }


    public int travea()
    {
        return 1;
    }
    List<clsBaiThi> l = new List<clsBaiThi>();
    //*******************Bat dau ham xao tron dap an****************************
    public List<clsBaiThi> hamRandomDapAn(List<clsBaiThi> ctch)
    {
        
        foreach (var item in ctch)
        {
            List<string> listswap = new List<string>();
            listswap.Add(item.Pa1);
            listswap.Add(item.Pa2);
            listswap.Add(item.Pa3);
            listswap.Add(item.Pa4);
            var rand = new Random();
            for (int i = listswap.Count - 1; i > 0; i--)
            {
                int n = rand.Next(i + 1);
                string temp = listswap[i];
                listswap[i] = listswap[n];
                listswap[n] = temp;


            }
            item.Pa1 = listswap[0];
            item.Pa2 = listswap[1];
            item.Pa3 = listswap[2];
            item.Pa4 = listswap[3];
        }
        return ctch;
 
    }
    //*******************Ket thuc ham xao tron dap an****************************


    public List<clsBaiThi> hamTronCauhoi(List<clsBaiThi> ctch)
    {
        var rand = new Random();
        for (int i = ctch.Count-1; i > 0; i--)
        {
            int n=rand.Next(i+1);
            clsBaiThi temp = ctch[i];
            ctch[i] = ctch[n];
            ctch[n] = temp;
        }
        return ctch;
 
    }
    [DataObjectMethod(DataObjectMethodType.Select, true)]
    public List<clsBaiThi> LayCHtuCTDT(string madt)
    { 
        //List<CauHoi> ch = new List<CauHoi>();
        var ctch = (from p in db.tbl_NGANHANGCAUHOIs
                    from q in db.tbl_CHITIETDETHIs
                    where p.MaCH == q.MaCH &&
                         q.MaDT == madt
                    select new clsBaiThi
                    {
                       
                        STT=string.Empty,
                        MaCH = p.MaCH,
                        Noidung = p.NoiDungCH,
                        Pa1 = p.DA1,
                        Pa2 = p.DA2,
                        Pa3 = p.DA3,
                        Pa4 = p.DA4,
                        Traloi = string.Empty
                    }).ToList();
        //foreach (var item in ctch)
        //{
        //    dsch.Add(item);
        //}
        List<clsBaiThi> danhsachCHdatron = hamTronCauhoi(ctch);
        return hamRandomDapAn(danhsachCHdatron); 
    }

    public List<clsBaiThi> getdata(string madt)
    {
        int stt=1;
        clsBaiThi bt = new clsBaiThi();
        l = LayCHtuCTDT(madt);
        foreach (var itemlist in l)
        {
            itemlist.STT1 = stt.ToString();
            //.STT1 = stt.ToString();
            stt++;
            
        }

        return l;
    }
    /*Ham lay ma bai thi*/
    public bool checkMaBT(string mabt)
    {
        var mabtInData = (from p in db.tbl_BAITHIs
                    where p.MaBT == mabt
                    select p.MaBT).FirstOrDefault();
        if (mabtInData!=null)
        {
            return false;
        }
        return true;
 
    }
    


}
